package com.ioidea.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ioidea.room.bean.GroupName
import com.ioidea.room.bean.GroupDeviceDao
import com.ioidea.room.bean.GroupStrategy
import com.ioidea.room.bean.GroupStrategyDao
import com.ioidea.room.bean.JKChildDevDao
import com.ioidea.room.bean.JkChildDev
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


@Database(
    entities =
    [
        JkChildDev::class,
        GroupName::class,
        GroupStrategy::class,
    ], version = 1, exportSchema = true
)
abstract class AppDataBase : RoomDatabase() {

    abstract fun getJKChildDevDao(): JKChildDevDao

    abstract fun getGroupDeviceDao(): GroupDeviceDao

    abstract fun getGroupStrategyDao(): GroupStrategyDao
}

@InstallIn(SingletonComponent::class)
@Module
object DBModule {

    const val DB_NAME = "TSmartPlc5"

    @Provides
    @Singleton
    fun provideProtocolDatabase(@ApplicationContext appContext: Context): AppDataBase {
        return Room.databaseBuilder(
            appContext,
            AppDataBase::class.java,
            DB_NAME
        ) //可以强制在主线程运行数据库操作
            .allowMainThreadQueries()
            //.fallbackToDestructiveMigration()
            //.addMigrations(com.ioidea.ioplant.room.dev.AppDevDataBase.MIGRATION_1_2)
            .build()
    }

    @Singleton
    @Provides
    fun getJKChildDevDao(db: AppDataBase): JKChildDevDao {
        return db.getJKChildDevDao()
    }

    @Singleton
    @Provides
    fun getGroupDeviceDao(db: AppDataBase): GroupDeviceDao {
        return db.getGroupDeviceDao()
    }

    @Singleton
    @Provides
    fun getGroupStrategyDao(db: AppDataBase): GroupStrategyDao {
        return db.getGroupStrategyDao()
    }
}
